package Presenter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Model.Game;
import Model.Player;

/**
 * Constructs a summary screen which shows at the end of each turn
 * 
 * @author Philip
 * 
 */
public class EndRoundScreen extends JPanel {

	private static final long serialVersionUID = 1L;
	private static final int SUBROW_OFFSET = 20;
	private final Game g;
	private final MainGameScreen gameScreen;
	private final int COLUMN_A_X = 70;
	private final int COLUMN_A_Y = 150;
	private final int ROW_OFFSET = 70;
	private final int COLUMN_OFFSET = 400;

	JButton but1, but2, but3, but4, but5, but6, but7, but8, but9, but0,
			butPlus, butMinus, clearAll;

	/**
	 * Constructor for the summary end of round screen. Includes an
	 * actionlistener which instantiates the next round.
	 * 
	 * @param g
	 * @param gameScreen
	 */
	public EndRoundScreen(Game g, final MainGameScreen gameScreen) {

		this.g = g;
		this.gameScreen = gameScreen;
		Player[] players = g.getPlayers();

		setBackground(Color.BLACK);
		setLayout(null);
		setPreferredSize(new Dimension(1200, 800));

		for (int i = 0; i < 4; i++) {
			JLabel playerName = new JLabel(players[i].getName());
			playerName.setFont(new Font("Helvetica", Font.BOLD, 20));
			playerName.setForeground(new Color(204, 153, 0));
			playerName.setSize(900, 30);
			playerName.setLocation(COLUMN_A_X, COLUMN_A_Y + i * ROW_OFFSET);
			this.add(playerName);
		}

		JLabel colonyLabel = new JLabel("COLONY ");
		colonyLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
		colonyLabel.setForeground(new Color(204, 153, 0));
		colonyLabel.setSize(900, 30);
		colonyLabel.setLocation(COLUMN_A_X, COLUMN_A_Y + 4 * ROW_OFFSET);
		this.add(colonyLabel);

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				JLabel asset;
				switch (j) {
				case 0:
					asset = new JLabel("MONEY");
					break;
				case 1:
					asset = new JLabel("LAND");
					break;
				case 2:
					asset = new JLabel("GOODS");
					break;
				case 3:
					asset = new JLabel("TOTAL");
					break;
				default:
					asset = new JLabel("ERROR");
					break;
				}

				asset.setFont(new Font("Helvetica", Font.BOLD, 12));
				asset.setForeground(new Color(204, 153, 0));
				asset.setSize(900, 18);
				asset.setLocation(COLUMN_A_X + COLUMN_OFFSET, COLUMN_A_Y + i
						* ROW_OFFSET + (j) * SUBROW_OFFSET);
				this.add(asset);
			}
		}

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				JLabel amount;
				switch (j) {
				case 0:
					amount = new JLabel(players[i].getMoney() + "");
					break;
				case 1:
					amount = new JLabel("PLAYER CLASS NEEDS GETLANDCOUNT()");
					break;
				case 2:
					amount = new JLabel(players[i].getEnergy()
							+ players[i].getFood() + players[i].getOre() + "");
					break;
				case 3:
					amount = new JLabel(players[i].getMoney()
							+ players[i].getEnergy() + players[i].getFood()
							+ players[i].getOre() + "");
					break;
				default:
					amount = new JLabel(players[i].getName());
					break;
				}
				amount.setFont(new Font("Helvetica", Font.BOLD, 12));
				amount.setForeground(new Color(204, 153, 0));
				amount.setSize(900, 18);
				amount.setLocation(COLUMN_A_X + COLUMN_OFFSET * 2, COLUMN_A_Y
						+ i * ROW_OFFSET + (j) * SUBROW_OFFSET);
				this.add(amount);
			}
		}

		JLabel title = new JLabel("STATUS SUMMARY " + g.getCurrentRound());
		title.setFont(new Font("Helvetica", Font.BOLD, 65));
		title.setForeground(new Color(204, 153, 0));
		title.setSize(900, 100);
		title.setLocation(175, 73);
		this.add(title);

		JButton nextRoundButton = new JButton("START NEXT ROUND");
		nextRoundButton.setFont(new Font("Helvetica", Font.BOLD, 25));
		nextRoundButton.setForeground(new Color(204, 153, 0));
		nextRoundButton.setSize(200, 40);
		nextRoundButton.setLocation(500, 500);
		final GameScreenListener showMap = new GameScreenListener();
		nextRoundButton.addActionListener(showMap);
		this.add(nextRoundButton);

	}

	/**
	 * ActionListener which calls the next round.
	 * 
	 * @author Philip
	 * 
	 */
	private class GameScreenListener implements ActionListener {
		public void actionPerformed(ActionEvent h) {

			try {
				System.out.println("Trying to start the next Round");
				gameScreen.getRound().startRound();
			} catch (Exception g) {
			}

		}
	}

}
